from collections import OrderedDict

class LRUCache:
    """维护一个缓存,总是保存 Least Recently Used """


    def __init__(self, capacity:int = 2):

        # 头部是最先插入的位置,尾部是新插入的元素.

        self.cache = OrderedDict()
        self.capacity = capacity



    def get(self, key):
        """根据是否存在key,
        返回值:
            如果不存在,返回None
            如果存在,返回value
        """
        if key not in self.cache:
            return None
        else:
            self.cache.move_to_end(key)  # 将访问的key移动到OrderedDict的末尾
            return self.cache[key]


    def delete(self,key):
        """从缓存中删除某值"""
        if key in self.cache:
            # 如果key已存在，先删除旧的键值对
            del self.cache[key]



    def add(self, key, value):
        """
        向缓存中存储数据.
        """
        if key in self.cache:
            # 如果key已存在，先删除旧的键值对
            del self.cache[key]
        self.cache[key] = value


    def evict(self):
        # 如果缓存已满，逐出最老的元素
        if len(self.cache) > self.capacity:
            # 逐出OrderedDict中的第一个元素，即最老的元素
            return self.cache.popitem(last=False)
        else:
            return False,False

    def show(self):
        """输出当前的所有数据"""
        print(dict(self.cache))


# Example usage:
if __name__ == "__main__":
    store = LRUCache(1)

    uuid = '4F909471-32BE-7C54-BE32-1D05C47CA876'
    uuid2 = '2F909471-32BE-7C54-BE32-1D05C47CA876'
    data = {'key1': 'value1', 'key2': 'value2','key3':['11212']}
    data2 = {'key1': 'val'}
    store.add(uuid, data)
    store.add(uuid2, data2)
    a,b = store.evict()
    print(a,b)
    a, b = store.evict()
    print(a, b)